package com.weavernorth.action.dataToMDM;

import com.weavernorth.humanresource.SalarRangeUtil;
import com.weavernorth.util.Logger4jUtil;
import com.weavernorth.util.MyCommonUtils;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.Iterator;

/**
 * @Author: zl
 * @Package: com.weavernorth.action.dataToMDM
 * @date: 2020/11/18 9:33
 * @description: 【品牌】供应商SAP系统维护申请流程数据传送至mdm
 */
public class gysDataToMDM implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        Logger log = Logger4jUtil.getLoggerByName("/action/gysDataToMDM/");
        BaseBean bb=new BaseBean();
        //推送
        SalarRangeUtil salarRangeUtil = new SalarRangeUtil();
        log.info("供应商流程数据传至MDM数据开始。。。");
        RecordSet rs= new RecordSet();
        RecordSet rs_new= new RecordSet();
        //请求id
        String strRequestid = requestInfo.getRequestid();
        log.info("流程requestid=" + strRequestid);

        String strTable = requestInfo.getRequestManager().getBillTableName();
        log.info("表名：" + strTable);
        String strSql="select a.* from "+strTable+" a where a.requestid="+strRequestid+"";
        log.info("执行查询语句："+strSql);
        rs.executeQuery(strSql);
        //String sapGysbm ="";
        String mdmCode ="";
        while (rs.next()){
            //获取当前流程供应商编码
            //sapGysbm = Util.null2String(rs.getString("gysbm"));
            mdmCode = Util.null2String(rs.getString("gysbm"));
           log.info("MDM主编码gysbm：" + mdmCode);
        }
        //报文请求体
        StringBuffer requestXml = new StringBuffer();
        //拼接请求体
        requestXml.append("<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:rec='http://www.example.org/receivebpm/'>" +
                "<soapenv:Header/>" + "<soapenv:Body>" + "<rec:receivebmp>" + " <DATAINFOS>" +
                "<PUUID></PUUID>");
        if(!"".equals(strRequestid)){
            //根据规划编码获取建模表数据
            String sqlHead="select a.ID,a.gysbm,a.gysmc,a.gysjc,a.gysdz,a.zhzbm,a.myhb,a.cgzz,c.HBJC,d.dm,a.swdjh," +
                    "a.kpdh,a.frdb,a.khbm,a.dq,a.yb from uf_gystzsj a " + //,c.code
                    //"left join uf_mdm_lc_data b on a.zhzbm = b.ID " +
                    "left join uf_hbdw c on a.cgddhb = c.ID " +
                    "left join uf_gjdm d on a.gjdm   = d.ID " +
                    " where a.lcid ='"+strRequestid+"'";
                    //"left join view_mdm_gys c on to_char(a.gysbm) = c.DESC56 ";
                //sqlHead += " where a.lcid ='"+strRequestid+"'";
            /*if(null!=mdmCode && mdmCode.length() > 1)
            {
                sqlHead += " where a.gysbm='"+mdmCode+"'";
                //strRequestid = sapGysbm;
            }else
            {
                sqlHead += " where a.lcid ='"+strRequestid+"'";
            }*/

            log.info("sql执行语句抬头："+sqlHead);
            rs_new.executeQuery(sqlHead);
            String ID = "";
            while (rs_new.next()) {
                ID = Util.null2String(rs_new.getString("ID"));
                log.info("模表数据ID：" + ID);
                String gysmc = Util.null2String(rs_new.getString("gysmc"));
                log.info("供应商名称：" + gysmc);
                String gysjc = Util.null2String(rs_new.getString("gysjc"));
                log.info("供应商简称：" + gysjc);
                String gysdz = Util.null2String(rs_new.getString("gysdz"));
                log.info("供应商地址：" + gysdz);
                String zhzbm = Util.null2String(rs_new.getString("zhzbm"));
                log.info("账户组：" + zhzbm);
                String myhb = Util.null2String(rs_new.getString("myhb"));
                log.info("贸易伙伴：" + myhb);
                String cgzz = Util.null2String(rs_new.getString("cgzz"));
                log.info("采购组织：" + cgzz);
                String HBJC = Util.null2String(rs_new.getString("HBJC"));
                log.info("采购订单货币：" + HBJC);
                String dm = Util.null2String(rs_new.getString("dm"));
                log.info("国家代码：" + dm);
                String swdjh = Util.null2String(rs_new.getString("swdjh"));
                log.info("税务登记号：" + swdjh);
                String kpdh = Util.null2String(rs_new.getString("kpdh"));
                log.info("开票电话：" + kpdh);
                String frdb = Util.null2String(rs_new.getString("frdb"));
                log.info("法人代表：" + frdb);
                String khbm = Util.null2String(rs_new.getString("khbm"));
                log.info("客户编码：" + khbm);
                String yb = Util.null2String(rs_new.getString("yb"));
                log.info("邮编：" + yb);
                String gysbm = Util.null2String(rs_new.getString("gysbm"));
                log.info("mdm主编码：" + gysbm);
                //String mdmCode = Util.null2String(rs_new.getString("code"));
                log.info("MDM code：" + mdmCode);
                //拼接XML
                //供应商名称
                requestXml.append("<DATAINFO>");
                requestXml.append("<CODE>"+mdmCode+"</CODE>");
                requestXml.append("<DESC1>" + gysmc + "</DESC1>");
                //供应商简称
                requestXml.append("<DESC2>"+gysjc+"</DESC2>");
                //供应商地址
                requestXml.append("<DESC3>"+gysdz+"</DESC3>");
                //账户组
                requestXml.append("<DESC4>"+zhzbm+"</DESC4>");
                //贸易伙伴
                requestXml.append("<DESC5>"+myhb+"</DESC5>");
                //采购组织
                requestXml.append("<DESC6>"+cgzz+"</DESC6>");
                //采购订单货币
                requestXml.append("<DESC8>"+HBJC+"</DESC8>");
                //国家代码DESC9
                requestXml.append("<DESC9>"+dm+"</DESC9>");
                requestXml.append("<DESC10></DESC10>");
                //税务登记号DESC11
                requestXml.append("<DESC11>"+swdjh+"</DESC11>");
                //税务登记号DESC12-15
                requestXml.append("<DESC12></DESC12><DESC13></DESC13><DESC14></DESC14><DESC15></DESC15>");
                //开票电话DESC16
                requestXml.append("<DESC16>"+kpdh+"</DESC16>");
                //法人代表DESC17
                requestXml.append("<DESC17>"+frdb+"</DESC17>");
                //客户编码DESC18
                requestXml.append("<DESC18>"+khbm+"</DESC18>");
                //出票方DESC19 状态DESC20
                requestXml.append("<DESC19></DESC19><DESC20></DESC20>");
                //编DESC21
                requestXml.append("<DESC21>"+yb+"</DESC21>");
                requestXml.append("<DESC22></DESC22><DESC23></DESC23>");
                requestXml.append("<DESC24></DESC24><DESC25></DESC25>");
                requestXml.append("<DESC26></DESC26>");
                requestXml.append("<DESC27></DESC27>");
                requestXml.append("<DESC28></DESC28><DESC29></DESC29>");
                requestXml.append("<DESC31></DESC31><DESC32></DESC32>");
                requestXml.append("<DESC33></DESC33>");
                requestXml.append("<DESC34></DESC34>");
                requestXml.append("<DESC35></DESC35><DESC36></DESC36><DESC37></DESC37>");
                requestXml.append("<DESC38></DESC38><DESC39></DESC39>");
                requestXml.append("<DESC40></DESC40><DESC41></DESC41><DESC43></DESC43>");
                requestXml.append("<DESC44></DESC44><DESC45></DESC45><DESC46></DESC46>");
                requestXml.append("<DESC47></DESC47><DESC48></DESC48><DESC49></DESC49>");
                requestXml.append("<DESC50></DESC50><DESC51></DESC51><DESC52></DESC52>");
                requestXml.append("<DESC53></DESC53><DESC54></DESC54><DESC55></DESC55>");
                //编DESC21
                requestXml.append("<DESC56>"+gysbm+"</DESC56>");
                requestXml.append("<DESC57></DESC57><DESC58>"+strRequestid+"</DESC58>");

                requestXml.append("<SPECIALITYCODES><A03>");
                String sqlA03 ="select * from uf_gystzsj_dt1 a left join uf_gjdm b on a.yxgj = b.id  where a.mainid='"+ID+"'";
                log.info("sql执行语句明细表1[a03]："+sqlA03);
                rs.executeQuery(sqlA03);
                while (rs.next())
                {
                    String yxhz = Util.null2String(rs.getString("yxhz"));
                    log.info("银行户主：" + yxhz);
                    String khyxbm = Util.null2String(rs.getString("khyxbm"));
                    log.info("开户银行编码：" + khyxbm);
                    String khyxms = Util.null2String(rs.getString("khyxms"));
                    log.info("开户银行描述：" + khyxms);
                    String yhgj = Util.null2String(rs.getString("dm"));
                    log.info("银行国家：" + yhgj);
                    String yxzh = Util.null2String(rs.getString("yxzh"));
                    log.info("银行账户：" + yxzh);
                    requestXml.append("<LIST><A0301>"+yxhz+"</A0301>");
                    requestXml.append("<A0302>"+khyxbm+"</A0302>");
                    requestXml.append("<A0303>"+khyxms+"</A0303>");
                    requestXml.append("<A0304>"+yhgj+"</A0304>");
                    requestXml.append("<A0305>"+yxzh+"</A0305>");
                    requestXml.append("<A0306></A0306>");
                    requestXml.append("</LIST>");
                }
                requestXml.append("</A03><A04>");
                String sqlA04 ="select gsdm,gsdmzt,b.zzkm from uf_gystzsj_dt2 a left join uf_mdm_lc_tykm_data b on a.tykm = b.id where a.mainid='"+ID+"'";
                log.info("sql执行语句明细表2[a04]："+sqlA04);
                rs.executeQuery(sqlA04);
                while (rs.next())
                {
                    String gsdm = Util.null2String(rs.getString("gsdm"));
                    log.info("公司代码：" + gsdm);
                    String gsdmzt = Util.null2String(rs.getString("gsdmzt"));
                    log.info("公司代码状态：" + gsdmzt);
                    String zzkm = Util.null2String(rs.getString("zzkm"));
                    log.info("统驭科目：" + zzkm);
                    requestXml.append("<LIST><A0401>"+gsdm+"</A0401>");
                    requestXml.append("<A0403>"+gsdmzt+"</A0403>");
                    requestXml.append("<A0404>"+zzkm+"</A0404>");
                    requestXml.append("</LIST>");
                }
                requestXml.append("</A04></SPECIALITYCODES>");
                requestXml.append("</DATAINFO>");
            }
        }
        requestXml.append("</DATAINFOS></rec:receivebmp> </soapenv:Body></soapenv:Envelope>");
        log.info("推送的xml:" + requestXml.toString());
        //获取地址
        //MDM测试地址：http://10.10.8.17:8777/gys_bpm_mdm
        //MDM正式地址: http://10.5.0.30:8777/gys_bpm_mdm
        String strSendUrl = bb.getPropValue("dataToMdm", "gysmdmUrl");

        log.info("mdm供应商接口地址："+strSendUrl);
        try {
            log.info("执行发送请求：");
            String responseXml = MyCommonUtils.sendPost(strSendUrl, requestXml.toString(), "utf-8");
            log.info("返回的xml:" + responseXml);
            //返回标识
            String strReturnStatus = "";
            //返回信息
            String strMessage = "";
            Document doc = null;//解析返回值开始
            doc = DocumentHelper.parseText(responseXml);
            Iterator<Element> its2 = doc.selectNodes("//STATUS").iterator();
            while (its2.hasNext()) {
                Element element = its2.next();
                strReturnStatus = element.getText();
                if (!"".equals(strReturnStatus)) {
                    break;
                }
            }
            log.info("返回值:" + strReturnStatus);
            //返回值失败时,则提示失败
            if ("E".equals(strReturnStatus)) {
                Iterator<Element> its3 = doc.selectNodes("//ERROTEXT").iterator();
                while (its3.hasNext()) {
                    Element element2 = its3.next();
                    strMessage = element2.getText();
                    if (!"".equals(strMessage)) {
                        break;
                    }
                }

            }
            if ("E".equals(strReturnStatus)) {
                requestInfo.getRequestManager().setMessagecontent(strMessage);
                return FAILURE_AND_CONTINUE;
            }
            else if ("".equals(strReturnStatus)||!"S".equals(strReturnStatus)) {
                requestInfo.getRequestManager().setMessagecontent("接口调用失败！");
                return FAILURE_AND_CONTINUE;
            }

        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessagecontent("程序异常："+e.toString());
            return FAILURE_AND_CONTINUE;
        }
        log.info("供应商流程数据传至MDM数据结束。。。");
        return SUCCESS;
    }
}
